package com.xiang.springboot.demo.mongodb.service.impl;

import com.xiang.springboot.demo.mongodb.entity.Entity1;
import com.xiang.springboot.demo.mongodb.entity.Entity2;
import com.xiang.springboot.demo.mongodb.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by admin on 2019/3/18.
 */
@Service
@Transactional
public class TestImpl implements ITestService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void testTransactionMananager() throws RuntimeException {
        Entity1 t1 = new Entity1("t1", 12);
        Entity2 t2 = new Entity2("t2", 10);
        mongoTemplate.save(t1);
        if (false) {
            throw new RuntimeException("error");
        }
        mongoTemplate.save(t2);
    }

    @Override
    public void insert() {
        Entity1 t1 = new Entity1("t1_insert", 12);
        t1 = mongoTemplate.save(t1);
        System.out.println(t1.getId());
    }

}
